<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.title" clearable placeholder="输入搜索标题" style="width: 300px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.articleType" clearable placeholder="类型" style="width: 150px;" class="filter-item">
          <el-option
            v-for="item in dict.article_types"
            :key="item.id"
            :label="item.label"
            :value="item.value" />
        </el-select>
        <rrOperation :crud="crud" />
        <el-button
          v-permission="['admin','syArticleBannerManage:add']"
          class="filter-item"
          size="mini"
          type="primary"
          icon="el-icon-plus"
          @click="toAddURL"
        >
        跳转新增
        </el-button>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="75%">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="文章标题" prop="title">
            <el-input v-model="form.title" />
          </el-form-item>
          <el-row>
            <el-col :span="6">
              <el-form-item label="类型" prop="articleType">
                <el-select v-model="form.articleType" clearable placeholder="类型" @change="typeSelect">
                  <el-option
                    v-for="item in dict.article_types"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6" v-show="isClassType">
              <el-form-item label="类别">
                <el-select v-model="form.classType" filterable placeholder="请选择">
                  <el-option
                    v-for="item in queryTypeOptions"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            
            <el-col :span="6">
              <el-form-item label="作者">
                <el-input v-model="form.author"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="是否跳转" prop="isUrl">
                <el-radio-group v-model="form.isUrl" @change="isUrlSelect">
                  <el-radio label="1">是</el-radio>
                  <el-radio label="0">否</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            
          </el-row> 
          <el-row>
            <el-col :span="10" v-show="isShowUrl">
              <el-form-item label="跳转链接">
                <el-input v-model="form.url"/>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="排序" prop="sort">
                <el-input v-model="form.sort" type="number"/>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="状态" prop="status">
                <el-radio-group v-model="form.status">
                  <el-radio label="1">正常</el-radio>
                  <el-radio label="0">禁用</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="图片URL" v-show="isImageUrl">
              <MaterialList
                      v-model="imageUrlArr"
                      style="width: 915px;"
                      type="image"
                      :num="1"
                      :width="150"
                      :height="150"
              />
          </el-form-item>
          <el-form-item label="正文" v-show="!isShowUrl">
            <ueditor-wrap v-model="form.content" :config="myConfig"  @beforeInit="addCustomDialog"  style="line-height: initial;width: 90%;"></ueditor-wrap>
          </el-form-item> 
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="columns.visible('title')" show-overflow-tooltip prop="title" label="标题" />
        <el-table-column v-if="columns.visible('imageUrl')" prop="imageUrl" label="图片URL">
          <template slot-scope="scope">
            <a :href="scope.row.imageUrl" style="color: #42b983" target="_blank" v-if="scope.row.imageUrl !='' && scope.row.imageUrl !=null">
              <img :src="scope.row.imageUrl" alt="点击打开" class="table-img" />
            </a>
            <div v-else></div>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('articleType')" prop="articleType" label="类型" width="80" >
          <template slot-scope="scope">
            <div>
              <el-tag>{{ dict.label.article_types[scope.row.articleType] }}</el-tag>
            </div>
            
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('isUrl')" prop="isUrl" label="是否跳转" width="80">
          <template slot-scope="scope">
            {{ dict.label.is_enable[scope.row.isUrl] }}
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('sort')" prop="sort" label="排序" width="70"/>
        <el-table-column v-if="columns.visible('status')" prop="status" label="状态" width="55">
            <template slot-scope="scope">
              <div>
                <el-tag v-if="scope.row.status == 1" :type="''">正常</el-tag>
                <el-tag v-else :type=" 'info' ">禁用</el-tag>
              </div>
            </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('createTime')" prop="createTime" label="添加时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-if="columns.visible('updateTime')" prop="updateTime" label="修改时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column v-permission="['admin','syArticleBannerManage:edit','syArticleBannerManage:del']" label="操作" width="250px" align="center">
          <template slot-scope="scope">
            <div style="display: flex;">
              <udOperation
                :data="scope.row"
                :permission="permission"
              />
              &nbsp;&nbsp;
              <el-button
                v-permission="['admin','syArticleBannerManage:edit']"
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="toUpdateURL(scope.row.id)"
              >
              跳转编辑
              </el-button>
            </div>
              
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSyArticleBannerManage from '@/api/syArticleBannerManage'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import MaterialList from "@/components/material";
import picUpload from '@/components/pic-upload';
import editor from '../../components/Editor';
import yamedit from '@/components/YamlEdit';
import UeditorWrap from 'vue-ueditor-wrap';


// crud交由presenter持有
const defaultCrud = CRUD({ title: '', url: 'api/syArticleBannerManage', sort: 'id,desc', crudMethod: { ...crudSyArticleBannerManage }})
const defaultForm = { id: null, title: null, imageUrl: null, content: null, author: 'yundangpu', articleType: null, isUrl: '0', url: null, visit: null, sort: null, status: '1', createTime: null, updateTime: null, classType: null }
export default {
  name: 'SyArticleBannerManage',
  components: { editor, picUpload, yamedit, UeditorWrap , pagination, crudOperation, rrOperation, udOperation ,MaterialList},
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ['article_types', 'is_enable', 'is_enable', 'agree_ments','common_problems'],
  data() {
    return {
      isShowUrl:false,
      isClassType:false,
      isImageUrl:true,
      imageUrlArr:[],
      permission: {
        add: ['admin', 'syArticleBannerManage:add'],
        edit: ['admin', 'syArticleBannerManage:edit'],
        del: ['admin', 'syArticleBannerManage:del']
      },
      rules: {
        id: [
          { required: true, message: '文章管理ID不能为空', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '文章标题不能为空', trigger: 'blur' }
        ],
        articleType: [
          { required: true, message: '文章类型', trigger: 'blur' }
        ],
        visit: [
          { required: true, message: '浏览次数不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '排序不能为空', trigger: 'blur' }
        ],
        isUrl: [
          { required: true, message: '是否跳转不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '正文否跳转不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '添加时间不能为空', trigger: 'blur' }
        ]
      },
      myConfig: {
        autoHeightEnabled: false, // 编辑器不自动被内容撑高
        initialFrameHeight: 500, // 初始容器高度
        initialFrameWidth: '100%', // 初始容器宽度
        UEDITOR_HOME_URL: '/UEditor/',
        serverUrl: '',
      },
      queryTypeOptions: []
    }
  },
  watch: {
    "imageUrlArr":function(val) {
      if (val) {
        this.form.imageUrl = val.join(",");
      }
    },
  },
  methods: {
    toAddURL(){
      this.$router.push({ path: '/shop/articleAdd' })
    },
    toUpdateURL(id){
      this.$router.push({ path: '/shop/articleEdit/'+id })
    },
    addCustomDialog () {
      window.UE.registerUI('yshop', function (editor, uiName) {
        let dialog = new window.UE.ui.Dialog({
          iframeUrl: '/yshop/materia/index',
          editor: editor,
          name: uiName,
          title: '上传图片',
          cssRules: 'width:1200px;height:500px;padding:20px;'
        });
        this.dialog = dialog;
        var btn = new window.UE.ui.Button({
          name: 'dialog-button',
          title: '上传图片',
          cssRules: `background-image: url(../../../assets/images/icons.png);background-position: -726px -77px;`,
          onclick: function () {
            dialog.render();
            dialog.open();
          }
        });

        return btn;
      }, 37);
    },
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      const query = this.query
      if (query.type && query.value) {
        this.crud.params[query.type] = query.value
      }else{
        delete this.crud.params.title
        delete this.crud.params.articleType
      }
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      this.form.isUrl = form.isUrl.toString();
      this.form.status = form.status.toString();
      this.typeSelect();
      this.isUrlSelect();
      if (form.imageUrl && form.id) {
        this.imageUrlArr = form.imageUrl.split(",");
      }else{
        this.imageUrlArr=[]
      }
    },
    typeSelect(){
      let that = this.form;
      if(that.articleType == '1'){//1：app协议
        this.isImageUrl = false;
        this.isClassType = true;
        this.queryTypeOptions = this.dict.agree_ments;
      }else if(that.articleType == '3'){//3：常见问题
        this.isImageUrl = true;
        this.isClassType = true;
        this.queryTypeOptions = this.dict.common_problems;
      }else{
        this.isClassType = false;
        this.isImageUrl = true;
      }
    },
    isUrlSelect(){
      let that = this.form;
      if(that.isUrl == '1'){//是否跳转（0：否1：是）
        this.isShowUrl = true;
      }else{
        this.isShowUrl = false;
      }
    }
  }
}



</script>

<style scoped>
  .table-img {
    display: inline-block;
    text-align: center;
    background: #ccc;
    color: #fff;
    white-space: nowrap;
    position: relative;
    overflow: hidden;
    vertical-align: middle;
    width: 32px;
    height: 32px;
    line-height: 32px;
  }
</style>
